package com.nolydake.swingnote.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import com.nolydake.swingnote.model.Note;

public class NoteEditDialog extends JDialog {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 5518561264535723500L;

    // 如果没有参数，则表示新增数据
    public NoteEditDialog(JFrame frame) {
        this(frame, null);
    }

    private Note note = null;
    private JFrame frame = null;

    // 如果有参数，则表示编辑数据
    public NoteEditDialog(JFrame frame, Note note) {
        super(frame, true);
        this.note = note;
        initGui();
    }

    private void initGui() {

        // 中间的面板
        JScrollPane jsp = new JScrollPane();
        JEditorPane jep = new JEditorPane();
        jep.setContentType("text/html");

        if (note != null) {
            jep.setText(note.content);
        }

        jsp.setViewportView(jep);
        this.add(jsp, BorderLayout.CENTER);

        // 下面的面板
        JPanel panel = new JPanel();
        JButton btnOK = new JButton("保存");
        JButton btnCancel = new JButton("取消");
        panel.add(btnOK);
        panel.add(btnCancel);
        this.add(panel, BorderLayout.SOUTH);

        this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                int opt = JOptionPane.showConfirmDialog(frame, "您确定要保存吗？\r\n  【是】:保存信息，并关闭\r\n  【否】:不保存信息\r\n【取消】:继续编辑",
                        "确认", JOptionPane.YES_NO_CANCEL_OPTION);
                if (JOptionPane.YES_OPTION == opt) {
                    
                    
                    
                    NoteEditDialog.this.dispose();
                } else if (JOptionPane.NO_OPTION == opt) {
                    NoteEditDialog.this.dispose();
                }
            };
        });
        this.setSize(new Dimension(800, 600));
        this.setLocationRelativeTo(null);
    }
}
